<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30vw" :close-on-click-modal="false" @close="close"
    :append-to-body="true">
    <el-form ref="form" :model="form" :rules="rules" label-width="0.8rem">
      <el-form-item label="角色名称">
        <el-input v-model.trim="form.juesemc" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model.trim="form.tips" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model.trim="form.paixu" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { doEdit,roleAdd,roleUpdate } from '@/api/userManagement'

export default {
  name: 'UserManagementEdit',
  data() {
    return {
      form: {},
      rules: {},
      title: '',
      dialogFormVisible: false,
    }
  },
  created() { },
  methods: {
    showEdit(row) {
      if (!row) {
        this.title = '新增'
      } else {
        this.title = '编辑'
        this.form = Object.assign({}, row)
      }
      this.dialogFormVisible = true
    },
    close() {
      this.$refs['form'].resetFields()
      this.form = this.$options.data().form
      this.dialogFormVisible = false
    },
    save() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          if(!this.form.id){
            const { msg } = await roleAdd(this.form)
            this.$baseMessage(msg, 'success')
          }else{
            const { msg } = await roleUpdate(this.form)
            this.$baseMessage(msg, 'success')
          }
          this.$emit('fetch-data')
          this.close()
        } else {
          return false
        }
      })
    },
  },
}
</script>
